libname a "D:\SAS\Dataset\CRSP\Monthly Return";


data a (keep=date cusip ticker prc PRIMEXCH ret SHROUT VWRETD);
set a.Return_1952_1979 a.Return_1980_2020 a.Return_2021_2022;
run;


libname b  "D:\SAS\Dataset\Fama French Factors";

data b (keep=dateff rf);
set b.ff5factormonthly;

data b (rename=(dateff=date));
set b;
run;

proc sort data=a; by date; 
proc sort data=b; by date;


data c; merge a b; by date;
run;

data c;
set c;
year=year(date);
month=month (date);
me=prc *shrout;
stockreturn=ret-rf;
MKTRF=VWRETD-rf;
run;



libname d "D:\SAS\Project\Persistence of MTB Ratio";

data d1 (keep=cusip fyear sgaintangible);
set d.sgaintangible;
run;

data d1 (rename=(fyear=year));
set d1;
run;


libname e  "D:\SAS\Dataset\Compustat\Compustat US";

data e1 (keep=DATADATE cusip at XRD sale COGS REVT INTPN XSGA ceq ni);
set e.Compustat1950_1979 e.Compustat1980_1989 e.Compustat1990_1995 e.Compustat1996_2000 e.Compustat2001_2005 
e.Compustat2006_2010  e.Compustat2011_2020 e.compustat2021_2023;
run;


data e1;
set e1;
if XRD=. then xrd=0;
year=year(DATADATE);
run;

proc sort data=d1; by cusip year;
proc sort data=e1; by cusip year;


data d2;
merge d1 e1; by cusip year;
run;

proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lat=lag(at);
if first.cusip then lat=.;
if sgaintangible<0 then sgaintangible=0;
if INTPN=. then INTPN=0;
run;

data d2;
set d2;
intangible=(sgaintangible+xrd)/((lat+at)/2);
assetgrowth=(at-lat)/lat;
profitability=(REVT-COGS-INTPN -XSGA)/ceq;
roe=ni/ceq;
run;


data c1 (keep=cusip year me);
set c; where month=12; 
run;

data d2;
set d2;
cusip=substr (cusip, 1, 8);
run;

proc sort data=d2; by cusip year;
proc sort data=c1; by cusip year;

data d2;
merge d2 c1; by cusip year;
mtb=me/ceq/1000;
run;


proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lintangible=lag(intangible);
lme=lag(me);
lmtb=lag(mtb);
lassetgrowth=lag(assetgrowth);
lprofitability=lag(profitability);
if first.cusip then lintangible =.;
if first.cusip then lme=.;
if first.cusip then lmtb=.;
if first.cusip then lassetgrowth=.;
if first.cusip then lprofitability=.;
run;


proc sort data=d2; by year lintangible;

proc rank data=d2 out=d3 group=5; by year; /* Note that although "group=5", SAS program actually divide lintangible into 4 big groups as a number of firms have zero value of intangible.*/
var lintangible lprofitability;
ranks elintangible elprofitability; 
run;

data d4 (keep=cusip year elintangible elprofitability);
set d3;
run;

proc sort data=c; by cusip year;
proc sort data=d4; by cusip year;

data f;
merge c d4; by cusip year;
run;



libname g "D:\SAS\Dataset\Fama French Factors";

data g1 (keep=DATEFF smb hml rmw cma umd);
set g.ff5factormonthly;

data g1 (rename=(dateff=date));
set g1;
run;


data g2;
set g.sgafactor;
run;

proc sort data=f; by date;
proc sort data=g1; by date;
proc sort data=g2; by date;


data h;
merge f g1 g2; by date;
run;


proc sort data=h; by date elprofitability elintangible; run;

proc means data=h; by date elprofitability elintangible;
output out= h1;
var stockreturn;
run;

data h1;
set h1;
where _STAT_= "MEAN";
run;

data h2 (keep=date year MKTRF smb hml RMW CMA UMD SGAfactor);
set h;
run;

proc sort data=h2 noduplicate; by date; run; 


proc sort data=h1; by date;
proc sort data=h2; by date;

data h3;
merge h1 h2; by date;
run;




/*Results for Table 7 Panel B*/

data h5;
set h3;where 1992<year <=2022 and elprofitability^=. and elintangible^=.; 
SGAfactorResidual=SGAfactor-(0.11794*MKTRF+0.22084*SMB-0.64662*HML-0.63268*RMW-0.00400*CMA);
RUN;
proc sort data=h5; by elprofitability elintangible;
proc reg data=h5; by elprofitability elintangible;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactorresidual;
run;


